Skip to main content

Gherkin Explanation

The structure of a test case is based on the plain-text language Gherkin. This contains several keywords that enable you to define structured test cases.

info

For comprehensive documentation of the Gherkin syntax, it is worth taking a look at the official Gherkin reference from the Cucumber project.

Keywords

Example

Feature: User Management
Only administrators can create or delete user accounts.

Background:
Given a administrator named "Alice"
And a user named "Bob"

Scenario: Administrator creates a new user
Given I am logged in as "Alice"
When I try to create a user with name "Charlie", email "charlie@example.com"
Then I should see "User 'Charlie' was created successfully."

Scenario: User attempts to create a new user
Given I am logged in as "Bob"
When I try to create a user with name "Dave", email "dave@example.com"
Then I should see "You are not allowed to create users."

Feature

The keyword Feature starts the description of exactly one test case. Such a test case is derived from the user journeys of the requirements (see here). A test case describes a software feature, in the example above the user management feature.

Scenario

A feature can consist of several scenarios. A scenario describes a single test case that can be specified precisely using the keywords Given, When, Then, And, and But.

In the example, there are two scenarios, “Administrator creates a new user” and “User attempts to create a new user,” which are then specified in more detail.

Given

Within a scenario, the context for the scenario is described using the keyword Given. In the first scenario, for example, this means that you are logged in as administrator “Alice.”

When

The When keyword can be used to describe the action that is performed in the scenario for the given context. In the example above, for example, the first scenario describes the action that the user “Alice” wants to create another user.

Then

The expected result is described after the keyword Then. For example, the message “User 'Charlie' was created successfully.” appears in scenario 1 of the example.

And, But

The keywords And and But can be used to expand the context (Given) or the result (Then) so that, for example, multiple results are expected (And) or a specific result is not expected (But).

Background

Since a feature can consist of multiple scenarios and these should share a context, it is possible to define a general context using the keyword Background instead of specifying the same context in each scenario. In the example, existing users are defined in general terms so that each scenario can use them.